<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('清单管理列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>清单名称：</label>
                            <input type="text" name="listName"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" data-toggle="modal" data-target="#myModal" shiro:hasPermission="questionlist:quelist:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="questionlist:quelist:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>

<div class="modal inmodal" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content animated bounceInRight">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                </button>
                <i class="fa fa-laptop modal-icon"></i>
                <h4 class="modal-title">创建清单</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label>清单名称</label>
                    <input type="text" id="form-quelist-add" placeholder="请输入您的清单名称" class="form-control">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                <button type="button" onclick="submitHandler()" class="btn btn-primary">保存</button>
            </div>
        </div>
    </div>
</div>

<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('questionlist:quelist:edit')}]];
    var removeFlag = [[${@permission.hasPermi('questionlist:quelist:remove')}]];
    var prefix = ctx + "questionlist/quelist";

    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            modalName: "清单管理",
            columns: [{
                checkbox: true
            },
                {
                    field: 'listName',
                    title: '清单名称'
                },
                {
                    field: 'createdAt',
                    title: '创建时间'
                },
                {
                    field: 'updatedAt',
                    title: '修改时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="selectQuestionsList(\'' + row.id + '\')"></i>选题</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function submitHandler() {
        var inputValue = $('#form-quelist-add').val();
        if (!inputValue) {
            alert("清单名称不能为空");
            return;
        }
        $.operate.save(prefix + "/add", { listName: inputValue });
    }

    function selectQuestionsList(listId) {
        console.log("进入选题页面")
        var options = {
            title: '选择题目',
            url: "demo/modal/list?listId=" + listId,
            btn: ['<i class="fa fa-check"></i> 确认', '<i class="fa fa-close"></i> 关闭'],
            yes: function yes() {
                console.log("点击确定")
                var selectedIds = localStorage.getItem('selectedIds');
                if (!selectedIds || (Array.isArray(selectedIds) && selectedIds.length === 0) || selectedIds === "") {
                    alert("没有选中的数据");
                } else {
                    var ids = JSON.parse(selectedIds);
                    // 发送到后端
                    console.log("ids: " + ids)
                    $.ajax({
                        url: prefix + "/detailsIds",
                        type: "POST",
                        contentType: "application/json",
                        data: JSON.stringify({ listId: listId, ids: ids }), // 将 listId 和 ids 一起发送
                        success: function(response) {
                            console.log("response: " + response)
                            // if (response.success) {
                            //     alert("操作成功！");
                            // } else {
                            //     alert("操作失败！" + response.message);
                            // }
                        }
                    });
                }
            }
        };
        $.modal.openOptions(options);
    }
</script>
</body>
</html>